package com.lanou.sinasports.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.Events;
import com.lanou.sinasports.greenevents.Eve;
import com.lanou.sinasports.utils.BitmapCache;
import com.lanou.sinasports.utils.VolleySingleton;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class EventsAdapter extends BaseAdapter {
    private Context context;
    private String urls[]={"http://n.sinaimg.cn/default/20150225/ppki-awzuney0261539.png",
            "","","","","","","","","","http://n.sinaimg.cn/default/20150819/-lM4-fxfxray5654363.png"};
    private List<Events> data;
    private VolleySingleton queue;
    ImageLoader imageLoader;
    RequestQueue imgQueue;
    private List<Eve> eve;
    private String s;

    public EventsAdapter(Context context, String s, List<Eve> eve) {
        this.context = context;
        this.s = s;
        this.eve = eve;
    }

    public EventsAdapter(Context context, List<Events> data) {
        this.context = context;
        this.data = data;
        imgQueue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(imgQueue,new BitmapCache());

    }




    @Override
    public int getCount() {
        return data != null?data.size():eve != null ? eve.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_gv_events,parent,false);
            myViewHolder=new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        if(data!=null && !data.isEmpty()) {
            myViewHolder.textView.setText(data.get(position).getName());


            myViewHolder.ivLogo.setDefaultImageResId(R.mipmap.ic_main_tab_match);
            myViewHolder.ivLogo.setErrorImageResId(R.mipmap.ic_main_tab_special);
            myViewHolder.ivLogo.setImageUrl(data.get(position).getLogoId(), imageLoader);
        }
        else {
            myViewHolder.textView.setText(eve.get(position).getName());

            myViewHolder.ivLogo.setDefaultImageResId(R.mipmap.ic_main_tab_match);
            myViewHolder.ivLogo.setErrorImageResId(R.mipmap.ic_main_tab_special);
            myViewHolder.ivLogo.setImageUrl(eve.get(position).getLogo(), imageLoader);




        }
        return convertView;
    }

    class MyViewHolder{
        TextView textView;
        NetworkImageView ivCLogo;
        NetworkImageView ivLogo;
        public MyViewHolder(View view) {
            textView= (TextView) view.findViewById(R.id.tv_events_title);
            ivCLogo= (NetworkImageView) view.findViewById(R.id.iv_clogo);
            ivLogo= (NetworkImageView) view.findViewById(R.id.iv_logo);
        }
    }
}
